##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from lisp.core.media import Media


class Collector(QtCore.QObject):

    onMediaAdded = QtCore.pyqtSignal(object)
    onMediaRemoved = QtCore.pyqtSignal(object)

    def __init__(self):
        super(Collector, self).__init__()
        self.media = []

    def addMedia(self, media):
        self.media.append(media)

        media.onPlay.connect(self.onPlay)
        media.onPause.connect(self.onPause)
        media.onStop.connect(self.onStop)

        self.onMediaAdded.emit(media)

    def removeMedia(self, media):
        if(media is not None):
            media.interrupt(False)
            self.media.remove(media)
            self.onMediaRemoved.emit(media)
        elif(None in media):
            self.media.remove(None)

    def resetCollector(self):
        for media in self.media:
            self.removeMedia(media)

    def onPause(self, media):
        if(media.conf['groups'] != []):
            self.pauseGroups(media.conf['groups'])

    def onPlay(self, media):
        if(media.conf['stopAll']):
            self.stopAll(media)
        if(media.conf['groups'] != []):
            self.startGroups(media.conf['groups'])

    def onStop(self, media):
        if(media.conf['groups'] != []):
            self.stopGroups(media.conf['groups'])

    def stopAll(self, excluded):
        for media in self.media:
            if(media != excluded and media.state != Media.NONE):
                media.stop()

    def pauseGroups(self, groups):
        for media in self.media:
            if(media.state != Media.PLAYING):
                continue
            for group in groups:
                if(group in media.conf['groups']):
                    media.pause()
                    break

    def stopGroups(self, groups):
        for media in self.media:
            if(media.state != Media.PLAYING and media.state != Media.PAUSED):
                continue
            for group in groups:
                if(group in media.conf['groups']):
                    media.stop()
                    break

    def startGroups(self, groups):
        for media in self.media:
            if(media.state != Media.STOPPED and media.state != Media.PAUSED):
                continue
            for group in groups:
                if(group in media.conf['groups']):
                    media.play()
                    break
